package com.inossem.warehousestorage_inossem.request.storage;

import android.app.Dialog;
import android.content.Context;

import com.android.volley.toolbox.JsonObjectRequest;
import com.google.gson.Gson;
import com.inossem.utils.log.LogUtils;
import com.inossem.warehousestorage_inossem.bean.storage.StorageManagePosiBean;
import com.inossem.warehousestorage_inossem.bean.storage.StorageManageRespBean;
import com.inossem.warehousestorage_inossem.constant.RequestConstant;
import com.inossem.warehousestorage_inossem.dialog.DialogUtils;
import com.inossem.warehousestorage_inossem.listener.GsonBeanListener;
import com.inossem.warehousestorage_inossem.listener.StringRequestListener;
import com.inossem.warehousestorage_inossem.manage.RequestManage;
import com.inossem.warehousestorage_inossem.model.StringResponse;
import com.inossem.warehousestorage_inossem.request.Request;

/**
 * Created by leij on 2017/6/12.
 */

public class StorageManagePosiRequest extends Request {
    public static String request(Context context, GsonBeanListener gsonBeanListener) {
        String tag;
        String url = RequestManage.getUrl(context,RequestConstant.URL_STORAGE_MANAGE_POSI_LIST);
        Dialog dialog = DialogUtils.getLoadingDialog(context, new DialogUtils.LoadingListener() {
            @Override
            public void onCancel() {
                LogUtils.i("cancel");
            }
        }, "请稍等");
        dialog.show();

        JsonObjectRequest request = RequestManage.getHttpsGet(context, url, new StringRequestListener() {
            @Override
            public void onSuccess(StringResponse stringRequestResponse) {
                Gson gson = new Gson();
                String json = stringRequestResponse.getResponseInfo();
                StorageManagePosiBean bean = gson.fromJson(json, StorageManagePosiBean.class);
                excuteJsonBean(context, bean, bean.getCode(), bean.getMsg(), gsonBeanListener);
                dialog.hide();
            }
        });
        tag = addRequest(context, request, url);
        return tag;
    }


    public static String submitPosi(Context context, StorageManagePosiBean.DataBean.InventoryAddressListBean bean, GsonBeanListener gsonBeanListener) {
        String tag;
        String url = RequestManage.getUrl(context,RequestConstant.URL_STORAGE_MANAGE_POSI_SUBMIT);
        Gson gson = new Gson();
        String body = gson.toJson(bean);
        Dialog dialog = DialogUtils.getLoadingDialog(context, null);
        dialog.show();
        JsonObjectRequest request = RequestManage.getHttpsPost(context, url,body,new StringRequestListener() {
            @Override
            public void onSuccess(StringResponse stringRequestResponse) {
                Gson gson = new Gson();
                String json = stringRequestResponse.getResponseInfo();
                StorageManageRespBean bean = gson.fromJson(json, StorageManageRespBean.class);
                excuteJsonBean(context, bean, bean.getCode(), bean.getMsg(), gsonBeanListener);
                dialog.hide();
            }
        });
        tag = addRequest(context, request, url);
        return tag;
    }
}
